package org.permission.utils;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//先判断用户是否访问的是匿名资源
		Set<String> keys = ResourceUtils.getPropertyKeys("annonymousURLS.properties");
		String url = request.getRequestURI();
		System.out.println(url);
		for (String string : keys) {
			if(url.contains(string)) {
				return true;
			}
		}
		//如果不是匿名资源，判断用户是否登录
		Object user = request.getSession().getAttribute(Constant.LOGIN_USER);
		if(user == null) {
			request.getRequestDispatcher("/sysUser/login").forward(request, response);
			return false;
		}
		return true;
	}
}
